package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.LocaleManager;
import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.ejb.TaskFacadeRemote;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.CommonDialog;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.Component;
import java.util.Map;
import javax.swing.JComponent;

/**
 * @author dimitri
 */
public class ChangePasswordDialog extends CommonDialog {

    public ChangePasswordDialog(Component parent) {
        super(parent, true);
        initComponents();
        getRootPane().setDefaultButton(btnChange);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        logger = new ge.telasi.tasks.ui.log.InlineLogger();
        btnClose = new javax.swing.JButton();
        btnChange = new javax.swing.JButton();
        titlePanel1 = new ge.telasi.tasks.ui.comp.TitlePanel();
        txtCurrentPassword = new ge.telasi.tasks.ui.comp.SuggestionPassword();
        txtNewPassword = new ge.telasi.tasks.ui.comp.SuggestionPassword();
        txtNewPassword2 = new ge.telasi.tasks.ui.comp.SuggestionPassword();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("პაროლის შეცვლა");

        jLabel1.setText("მიმდინარე პაროლი:");

        jLabel2.setText("ახალი პაროლი:");

        jLabel3.setText("პაროლის დადასტურება:");

        btnClose.setText("გაუქმება");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnChange.setText("შეცვლა");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        titlePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons48x48/stock_lock.png"))); // NOI18N
        titlePanel1.setSubTitle("ჩაწერეთ ძველი პაროლი და შემდეგ ორჯერ ახალი");
        titlePanel1.setTitle("პაროლის შეცვლა");

        txtCurrentPassword.setName("CurrentPassword"); // NOI18N
        txtCurrentPassword.setSuggestion("ჩაწერეთ თქვენი ძველი პაროლი");

        txtNewPassword.setName("User.password"); // NOI18N
        txtNewPassword.setSuggestion("აარჩიეთ ახალი პაროლი");

        txtNewPassword2.setName("User.password2"); // NOI18N
        txtNewPassword2.setSuggestion("დაადასტურეთ ახალი პაროლი");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(logger, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCurrentPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtNewPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtNewPassword2, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnClose)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                        .addComponent(btnChange)))
                .addContainerGap())
            .addComponent(titlePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titlePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCurrentPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNewPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logger, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnChange))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        if (validateForm()) {
            String currentPassword = new String(txtCurrentPassword.getPassword());
            String newPassword = new String(txtNewPassword.getPassword());
            Platform pl = Platform.getDefault();
            try {
                TaskFacadeRemote facade = pl.getFacade();
                Map credentials = pl.getCredentials(currentPassword);
                facade.changePassword(credentials, newPassword);
                pl.changePassword(newPassword);
                dispose();
            } catch (Exception ex) {
                LoggerUtils.manageException(this, logger, ex);
            }
        }
}//GEN-LAST:event_btnChangeActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChange;
    private javax.swing.JButton btnClose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private ge.telasi.tasks.ui.log.InlineLogger logger;
    private ge.telasi.tasks.ui.comp.TitlePanel titlePanel1;
    private ge.telasi.tasks.ui.comp.SuggestionPassword txtCurrentPassword;
    private ge.telasi.tasks.ui.comp.SuggestionPassword txtNewPassword;
    private ge.telasi.tasks.ui.comp.SuggestionPassword txtNewPassword2;
    // End of variables declaration//GEN-END:variables

    protected JComponent[] allComponents() {
        return new JComponent[] {txtCurrentPassword, txtNewPassword, txtNewPassword2};
    }

    protected boolean validateForm() {
        LoggerUtils.clearLogger(logger, allComponents());
        try {
            validateWithException();
            return true;
        } catch (Exception ex) {
            LoggerUtils.manageException(this, logger, ex, allComponents());
            return false;
        }
    }

    protected void validateWithException() {
        String currentPassword = new String(txtCurrentPassword.getPassword());
        String newPassword = new String(txtNewPassword.getPassword());
        String newPassword_confirm = new String(txtNewPassword2.getPassword());
        LocaleManager lm = Platform.getDefault().getLocaleManager();
        if (currentPassword == null || currentPassword.trim().length() == 0) {
            String msg = lm.getMessage("ChangePasswordDialog.emptyCurrentPassword");
            throw new TaskException(msg, "CurrentPassword");
        }
        if (newPassword == null || newPassword.trim().length() == 0) {
            String msg = lm.getMessage("ChangePasswordDialog.emptyNewPassword");
            throw new TaskException(msg, "User.password");
        }
        if (newPassword_confirm == null || newPassword_confirm.trim().length() == 0) {
            String msg = lm.getMessage("ChangePasswordDialog.emptyNewPassword_confirm");
            throw new TaskException(msg, "User.password2");
        }
        if (!newPassword.equals(newPassword_confirm)) {
            String msg = lm.getMessage("ChangePasswordDialog.passwordNotMatch");
            throw new TaskException(msg, "NewPassword");
        }
    }

}
